package user;

import java.sql.*;

public class DisplayCustomerInvoice
{
	//Display name, phone, address of a customer in DisplayInvoicePage.jsp
	
	/*
	public static String GetCustomerInvoiceInfo(String username, String address)
	{
		String cid = LoginValidation.getUserId(username);
		DBConnector db = DBConnector.getDBConnector();		
		
		//String statement = "select cus_name, cus_phone, cus_address from customer where cus_ID = " + cid;
		//db.prepareStatement(statement);
		ResultSet CustomerInvoiceInfo = db.query(statement);
		
		return DisplayCustomerInvoiceItems(CustomerInvoiceInfo, address);		
	}	
	*/
	
	/*
	private static String DisplayCustomerInvoiceItems(String name, String address, String phone)
	{
		StringBuffer sBuf = new StringBuffer();
		try {	
			
			if (CustomerInvoiceInfo != null)
			{
				sBuf.append("<table width = 50% align = center border = 1><tr>");
				sBuf.append("<tr bgcolor=\"#666666\">");
		        sBuf.append("<th width=\"100\" bgcolor=\"#CCCCCC\" scope=\"col\">Name</th>");
		        sBuf.append("<th width=\"100\" bgcolor=\"#CCCCCC\" scope=\"col\">Phone</th>");
		        sBuf.append("<th width=\"100\" bgcolor=\"#CCCCCC\" scope=\"col\">Address</th>");
		        sBuf.append("</tr>");
				
				while (CustomerInvoiceInfo.next()){
		
					
					sBuf.append("<tr><td>"+
							CustomerInvoiceInfo.getString(1)+"</td><td>"+
							CustomerInvoiceInfo.getString(2)+"</td><td>"
							);
					if (address == null)
						sBuf.append(CustomerInvoiceInfo.getString(3)+"</td>"+"</tr>");
					else
						sBuf.append(address + "</td>" + "</tr>");
				}
				sBuf.append("</table>");
			}
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			//System.out.println("The connection is not established");
			return "";
		}finally{
		}	
	}
	*/
	
	
	public static String GetContainInfo(String username)
	{		
		String cid = LoginValidation.getUserId(username);
		DBConnector db = DBConnector.getDBConnector();			
		
		String AllOrderForms = "select form_ID from order_form where cus_ID = " + cid + " order by form_time desc";
		System.out.println("Username here : " + cid);
		db.prepareStatement(AllOrderForms);
		ResultSet AllOrderFormsResult = db.queryPstmt();
		try {
			if (AllOrderFormsResult != null)
			{
				
				AllOrderFormsResult.next();						
				
				String FormID = AllOrderFormsResult.getString(1);
				//System.out.println("Look at here : " + FormID); //test
				String statement = "select food_name, order_quantity, food_price, food.food_ID from food, contain, order_form" +
				" where contain.food_ID = food.food_ID and contain.form_ID = order_form.form_ID and " +
				"order_form.form_ID = " + FormID;
				//System.out.println("Statement here : " + statement);
				db.prepareStatement(statement);
				ResultSet ContainInfo = db.queryPstmt();
				return DisplayContainItems(ContainInfo);
			}
		} catch (SQLException e) {
			e.printStackTrace();	
			System.out.println("DisplayCheckoutPage Throw Exception");
		}				
		return null;
	}	
	
	private static String DisplayContainItems(ResultSet ContainInfo)
	{
		StringBuffer sBuf = new StringBuffer();
		try {	
			
			if (ContainInfo != null)
			{
				sBuf.append("<table width = 50% align = center border = 1><tr>");
				sBuf.append("<tr bgcolor=\"#666666\">");
		        sBuf.append("<th width=\"100\" bgcolor=\"#CCCCCC\" scope=\"col\">Name</th>");
		        sBuf.append("<th width=\"100\" bgcolor=\"#CCCCCC\" scope=\"col\">Phone</th>");
		        sBuf.append("<th width=\"100\" bgcolor=\"#CCCCCC\" scope=\"col\">Address</th>");
		        sBuf.append("</tr>");
				
				while (ContainInfo.next()){
		
					
					sBuf.append("<tr><td>"+
							ContainInfo.getString(1)+"</td><td>"+
							ContainInfo.getString(2)+"</td><td>"+
							ContainInfo.getString(3)+"</td>"+"</tr>"
							);
				}
				sBuf.append("</table>");
			}
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			//System.out.println("The connection is not established");
			return "";
		}finally{
		}	
	}
	

	
}
